package me.ahoukuse.koala.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import me.ahoukuse.koala.dao.ShareDao;
import me.ahoukuse.koala.dao.SharedFileDao;
import me.ahoukuse.koala.po.SharedFileDetail;
import me.ahoukuse.koala.po.SharedFiles;
import me.ahoukuse.koala.service.SharedService;
import me.ahoukuse.koala.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SharedServiceImpl implements SharedService {

    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int LENGTH = 6;

    @Autowired
    SharedFileDao sharedFileDao;

    @Autowired
    ShareDao shareDao;

    @Override
    public JSONObject newShare(int userId, String sharedCode, List<String> sharedFile) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw new RuntimeException();
        }
        HttpServletRequest request = requestAttributes.getRequest();
        SharedFiles sharedFiles = new SharedFiles();
        sharedFiles.setUserId(userId);
        sharedFiles.setSharedCode(sharedCode);
        sharedFiles.setSharedDateTime(new Date());
        sharedFiles.setPassword("");
        shareDao.insertOne(sharedFiles);
        List<SharedFileDetail> sharedFileDetails = new ArrayList<>();
        sharedFile.forEach(s -> {
            SharedFileDetail sharedFileDetail = new SharedFileDetail();
            sharedFileDetail.setSharedCode(sharedCode);
            sharedFileDetail.setFileId(Integer.parseInt(s));
            sharedFileDetail.setUserId(userId);
            sharedFileDetails.add(sharedFileDetail);
        });
        sharedFileDao.insertBatch(sharedFileDetails);
        JSONObject res = MessageUtil.msg(200, "Ok");
        JSONObject obj = new JSONObject();
        obj.put("sharedCode", sharedCode);
        obj.put("shareUrl", request.getContextPath()+"/testShare/"+sharedCode);
        res.put("data", obj);
        return res;
    }

    @Override
    public JSONObject newShare(int userId, List<String> sharedFile) {
        String sharedCode = generateRandomString();
        return newShare(userId, sharedCode, sharedFile);
    }

    public static String generateRandomString() {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }

        return sb.toString();
    }

    @Override
    public JSONObject getSharedFiles(String sharedCode) {
        JSONObject res = MessageUtil.msg(200, "Ok");
        return res;
    }

    @Override
    public JSONObject getSharedByUserId(int userId) {
        List<SharedFiles> sharedFiles = shareDao.getSharedByUserId(userId);
        JSONObject shared = new JSONObject();
        shared.put("shared", sharedFiles);
        JSONObject res = MessageUtil.msg(200,"Ok");
        res.put("data", shared);
        return res;
    }

    @Override
    public JSONObject updateSharedBySharedCode(SharedFiles sharedFiles) {
        shareDao.updateSharedBySharedCode(sharedFiles);
        return MessageUtil.msg(200, "Ok");
    }

    @Override
    public JSONObject getSharedBySharedCode(String sharedCode) {
        SharedFiles sharedFiles = shareDao.getSharedBySharedCode(sharedCode);
        JSONObject res = MessageUtil.msg(200, "Ok");
        res.put("data", sharedFiles);
        return res;
    }
}
